package cn.gan.loveofsoul.commons.utils;

/**
 * @author Gwh 2023/02/10 16:50
 * @DESCRIPTION 排序算法
 */
public class SortUtils {

    /**
     * 1.冒泡排序
     * @param nums
     * @return
     */
    public static void bubbleSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if (nums[j] > nums[j+1]) {
                    swap1(nums, j, j+1);
                }
            }
        }
    }

    /**
     * 2. 快速排序
     * @param nums
     * @param l
     * @param r
     */
    public static void quickSort(int[] nums, int l, int r) {
        if (l > r) {
            return;
        }
        int index = swap2(nums, l, r);
        quickSort(nums, l, index - 1);
        quickSort(nums, index + 1, r);
    }

    /*public static void insertionSort(int[] nums) {
        for (int i = 1;i<nums.length;i++) {
            int temp = nums[i];
            for (int j=i -1;j<nums.length && nums[j]) {

            }
        }

        if (nums[1] <= nums[0]) {

        }
    }*/

    // 元素互换位置
    private static void swap1(int[] nums, int i, int j) {
        // 位运算
        nums[i] ^= nums[j];
        nums[j] ^= nums[i];
        nums[i] ^= nums[j];
    }

    // 快排交换
    private static int swap2(int[] nums, int l, int r) {
        int pivot = nums[l];
        while (l < r) {
            while (l < r && nums[r] > pivot) {
                r--;
            }
            nums[l] = nums[r];
            while (l < r && nums[l] <= pivot) {
                l++;
            }
            nums[r] = nums[l];
        }
        nums[l] = pivot;
        return l;
    }

    /*public static void main(String[] args) throws ParseException {
        int[] a = {1, 3, 7, 4, 9, 0};
        Arrays.stream(a).filter(num -> !(3 == num)).collect(Collection)
        // bubbleSort(a);
        // quickSort(a, 0, a.length - 1);

        for (int num : a) {
            System.out.println(num);
        }
    }*/

    /*public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDateString = "2021-12-09 14:00:00";
        System.out.println("当前时间为:" + nowDateString);
        try {
            Date sourceDate = sdf.parse(nowDateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sourceDate);
            calendar.add(Calendar.DATE,+150);
            Date targetDate = calendar.getTime();
            System.out.println("加150天后的日期:"+sdf.format(targetDate));

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }*/

}
